package com.ngu.util;

import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* @Description:    url请求登录拦截器
* @Author:         hq
* @CreateDate:     2019/10/2 2019/10/2
* @UpdateUser:     hq
* @UpdateDate:     2019/10/2 2019/10/2
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
public class LoginInterceptor extends HandlerInterceptorAdapter {

    /**
     * 在请求处理之前进行调用（Controller方法调用之前）
     * 基于URL实现的拦截器
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getServletPath();
        System.out.println(path);
        if (path.matches(".*/((.css)|(.js)|(images)|(login)|(anon)|(test)|(public)).*")) {
            //不需要的拦截直接过
            return true;
        } else {
            // 这写你拦截需要干的事儿，比如取缓存，SESSION，权限判断等
            Object obj = request.getSession().getAttribute("SysUser");
            if(!ObjectUtils.isEmpty(obj)){
            return true;
            }else{
                System.out.println("请求被拦截了！");
                int a = 1/0;
                return  false;
            }


        }
    }


}
